package com.fitmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fitmanagement.entity.Users;
import com.fitmanagement.mapper.UsersMapper;
import com.fitmanagement.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虚空
 * @since 2024-10-25
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Override
    public Boolean removeByUserId(int id) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Users> userID = queryWrapper.eq("UserID", id);
        return this.remove(userID);
    }

    @Override
    public List<Map<String, Object>>  SearchByName(String username) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Users> username1 = queryWrapper.like("Username", username);
        return this.listMaps(username1);
    }

    @Override
    public boolean updateUser(int id, String username, String password, String email, LocalDateTime localDateTime, int Gender) {

        UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("UserID", id);

        // 创建要更新的 User 对象
        Users userToUpdate = new Users();
        userToUpdate.setUsername(username);
        userToUpdate.setPassword(password);
        userToUpdate.setEmail(email);
        userToUpdate.setGender(Gender);
        userToUpdate.setDateOfBirth(LocalDate.from(localDateTime));
        return this.update(userToUpdate, updateWrapper);

    }

    @Override
    public Users findByEmail(String email) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Email", email); // 确保字段名与数据库表列名一致
        System.out.println("查询结果：" + this.getOne(queryWrapper, false));
        return this.getOne(queryWrapper, false); // 第二个参数为 false，避免异常
    }



}
